<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use addons\shopro\model\UserConsumptionLogModel;
use app\admin\model\user\Groupwatercoupon;
use app\common\model\UserWaterCouponModel;
use app\common\model\YajinLogModel;

class UserWalletLog extends Base
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function index()
    {
        $params = $this->request->get();
        $wallet_type = $params['wallet_type'] ?? 'money';

        if (!in_array($wallet_type, ['money', 'score','yajin'])) {
            $this->error('参数错误');
        }
        if($wallet_type == 'money'){
            $msg = '钱包记录';
        }elseif ($wallet_type == 'score'){
            $msg = '积分记录';
        }else{
            $msg = '押金记录';
        }
        if($wallet_type == 'yajin'){
            $data = YajinLogModel::where(['user_id'=>$this->auth->id])->order('id desc')->paginate(10);
            $data = [
                'wallet_logs' => $data,
                'income' => '',
                'expend' => '',
            ];
        }else{
            $data = \addons\shopro\model\UserWalletLog::getList($params);
        }
        $this->success(($msg), $data);
    }

    /**
     * @menu:水票
     */
    public function water_coupon(){
       $type  = input('type');
       if($type == 1){
           $this->user_water_coupon();
       }else{
           $this->group_water_coupon();
       }
    }

    public function user_water_coupon(){
        $model = new UserWaterCouponModel();
        $data = $model->with(['goods'=>function($query){
            $query->field('id,image,title,subtitle,dispatch_type');
        }])->where(['user_id'=>$this->auth->id])->order('id desc')->paginate();
        $this->success($data);
    }

    public function group_water_coupon(){
        $model = new Groupwatercoupon();
        $data = $model->with(['goods'])->where(['group_id'=>$this->auth->group_id])->order('id desc')->paginate();
        $this->success($data);
    }

    /**
     * @menu:水票使用记录
     */
    public function water_coupon_log(){
        $model = new UserConsumptionLogModel();
        $water_id = input('water_id',0);
        $type     = input('type');
        $where['user_id'] = $this->auth->id;
        if($water_id){
            $where['goods_id'] = $water_id;
        }
        if($type == 1){
            $where['type'] = 'user_water_coupon';
        }elseif($type == 2){
            $where['type'] = 'group_water_coupon';
        }
        $data = $model->with(['goods'=>function($query){
            $query->field('id,image,title,subtitle,dispatch_type');
        }])->where($where)->order('id desc')->paginate();
        $this->success($data);
    }

}
